'use strict';

const Controller = require('egg').Controller;

const {createToken,createId}=require("../utils/util")

class User extends Controller {

  // 模糊搜索
  // async show(){
  //   const {ctx}=this
  //   const {val}=ctx.request.query
  //   console.log(val);
    // const result=await this.service.user.filterService(val)
    // console.log(result);
    // if(result.length){
    //   ctx.body={
    //     code:"1001",
    //     msg:"查看数据成功",
    //     result
    //   }
    // }else{
    //   ctx.body={
    //     code:"1002",
    //     msg:"查看数据失败"
    //   }
    // }
  // }

  // 查看
  async show(){
    const {ctx}=this
    const {id}=ctx.params
    const result=await this.service.user.showService(id)
    if(result.length){
      ctx.body={
        code:"1001",
        msg:"查看数据成功",
        result
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"查看数据失败"
      }
    }
  }

  // 编辑
  async update(){
    const {ctx}=this
    const {id}=ctx.params
    console.log(id);
    const {username,nickname}=ctx.request.body
    const result=await this.service.user.editService(username,nickname,id)
    if(result.affectedRows){
      ctx.body={
        code:"1001",
        msg:"编辑成功"
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"编辑失败"
      }
    }
  }

   // 删除
  async destroy(){
    const {ctx}=this
    const {id}=ctx.params
    console.log(id);
    const result=await this.service.user.destroyService(id)
    if(result.affectedRows){
      ctx.body={
        code:"1001",
        msg:"删除成功"
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"删除失败"
      }
    }
  }

  // 添加
  async create(){
    const {ctx}=this
    const {username,nickname,age}=ctx.request.body
    const id=createId(username)
    const result=await ctx.service.user.createService(id,username,nickname,age)
    if(result.affectedRows){
      ctx.body={
        code:"1001",
        msg:"添加成功"
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"添加失败"
      }
    }
  }

  // 查看所有
  async index(){
    const {ctx}=this
    const result=await ctx.service.user.indxService()
    if(result.length){
      ctx.body={
        code:"1001",
        msg:"获取数据成功",
        result
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"获取数据失败"
      }
    }
  }

  // 注册
  async register(){
    const {ctx}=this
    const {username,pwd}=ctx.request.body
    const flag=await this.service.user.changeName(username)
    console.log(flag);
    if(flag.length){
      ctx.body={
        code:"1002",
        msg:"用户名已被注册"
      }
    }else{
      const id=createId(username)
      const result=await this.service.user.registerService(id,username,pwd)
      if(result.affectedRows){
        ctx.body={
          code:"1001",
          msg:"注册成功"
        }
      }else{
        ctx.body={
          code:"1002",
          msg:"注册失败"
        }
      }
    }
  }

  // 登录
  async login(){
      const {ctx}=this
      // 解析用户名和密码
      const {username,pwd}=ctx.request.body
      // 数据库查找  配置mysql
      const result=await this.service.user.loginService(username,pwd)
      const token=createToken(result[0].id)
      if(result.length){
        ctx.body={
          code:"1001",
          msg:"登录成功",
          token
        }
      }else{
        ctx.body={
          code:"1002",
          msg:"登录失败"
        }
      }
  }
}

module.exports = User;